import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  giftList: {},
  currentGift: {},

  categories: []
};

const getters = {
  giftList: state => state.giftList,
  categories: state => state.categories,
  currentGift: state => state.currentGift,
  searchGiftCondition: state => state.searchGiftCondition
};

const actions = {
  getGiftPageS({ commit }, param) {
    return Api.getGiftPage(param).then(ret => {
      commit(types.UPDATE_GIFT_LIST, ret.data.data);
      return ret;
    });
  },
  getAllCategoryS({ commit }, param) {
    return Api.getAllCategory(param).then(ret => {
      commit(types.UPDATE_GIFT_CATEGORY, ret.data.data);
      return ret;
    });
  },
  getGiftS({ commit }, id) {
    return Api.getGift({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_GIFT, ret.data.data);
      return ret;
    });
  },
  addOrUpdateGiftS({ commit }, payload) {
    return Api.addOrUpdateGift({ body: payload }).then(ret => {
      return ret;
    });
  },
  deleteGiftS({ commit }, param) {
    return Api.deleteGift(param).then(ret => {
      return ret;
    });
  },
  topGiftS({ commit }, param) {
    return Api.topGift({ id: param }).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_GIFT](state, payload) {
    state.currentGift = {
      title: "",
      subTitle: "",
      slide: [],
      orgPrice: 0,
      detailContent: "",
      category: "",
      repertory: 0,
      minScore: 0,
      status: "EDIT"
    };
  },
  [types.UPDATE_CURRENT_GIFT](state, payload) {
    console.log(payload);
    state.currentGift = payload;
  },
  [types.UPDATE_GIFT_LIST](state, payload) {
    state.giftList = payload;
  },
  [types.UPDATE_GIFT_CATEGORY](state, payload) {
    state.categories = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
